package com.example.administrator.leilihong;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ContentProvideActivity extends AppCompatActivity {
        private TextView tv;
        String columns = ContactsContract.Contacts.DISPLAY_NAME;
    private CharSequence queryData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_provide);
        tv = findViewById(R.id.tv);
        tv.setText(getQueryData());

    }

    public CharSequence getQueryData() {
        StringBuilder stringBuilder = new StringBuilder();
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
        int displayNameIndex = cursor.getColumnIndex(columns);
        for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
            String displayname = cursor.getString(displayNameIndex);
            stringBuilder.append(displayname+"\n");


        }
        cursor.close();

        return stringBuilder.toString();
    }
}
